#include "tools.h"
#include <chrono>
#include <sstream>
using namespace std;
extern "C"
{
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavutil/rational.h>
}

namespace lcf
{

    // 根据时间基数计算
    long long XRescale(long long pts,
                       AVRational *src_time_base,
                       AVRational *des_time_base)
    {
        return av_rescale_q(pts, *src_time_base, *des_time_base);
    }
    void PrintErr(int err)
    {
        if (err == 0)
            return;
        char buf[1024] = {0};
        av_strerror(err, buf, sizeof(buf) - 1);
        cerr << buf << endl;
    }

    void XFreeFrame(AVFrame **frame)
    {
        if (!frame || !(*frame))
            return;
        av_frame_free(frame);
    }

    void MSleep(unsigned int ms)
    {
        auto beg = clock();
        for (int i = 0; i < ms; i++)
        {
            this_thread::sleep_for(std::chrono::seconds(1));
            if ((clock() - beg) / (CLOCKS_PER_SEC / 1000) >= ms)
                break;
        }
    }

    long long NowMs()
    {
        return clock() / (CLOCKS_PER_SEC / 1000);
    }

}
